<template>
  <div class="app-container">
    <!--查询-->
    <el-form :inline="true">

      <el-form-item label=" 角色名称" prop="roleName">
        <el-input
          v-model="searchForm.roleName"
          placeholder=" 角色名称"
          clearable
          @blur="fetchList"
          @keyup.enter.native="fetchList"
        />
      </el-form-item>

      <el-form-item label=" 角色标识" prop="roleTag">
        <el-input
          v-model="searchForm.roleTag"
          placeholder=" 角色标识"
          clearable
          @blur="fetchList"
          @keyup.enter.native="fetchList"
        />
      </el-form-item>
      <search-buttons
        :fetch-data="fetchList"
        :export-flag="false"
        :add-fun="addRole"
      />
    </el-form>
    <!--表格-->
    <el-table :data="tableData" border stripe highlight-current-row @current-change="handleCurrentChange">
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="roleName" label=" 角色名称" />
      <el-table-column prop="roleTag" label=" 角色标识" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <operation-buttons
            :row="scope.row"
            :show-detail="false"
            :edit-fun="editRole"
            :del-fun="delSysRoleById"
          />
          <el-dropdown style="margin-left: 5px">
            <el-button
              type="primary"
              size="mini"
            >
              更多<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="showRoleMenu(scope.row.id)">角色菜单</el-dropdown-item>
              <el-dropdown-item @click.native="showUserRole(scope.row.id)">用户角色</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <!-- 分页 -->
      <pagination
        :page.sync="searchForm.currentPage"
        :total="total"
        :limit.sync="searchForm.pageSize"
        @pagination="fetchList"
      />
    </div>
  </div>
</template>
<script>
import sysRole from '@/api/sysRole'
import Pagination from '@/components/Pagination/index'
import SearchButtons from '@/components/SearchButtons/index'
import OperationButtons from '@/components/OperationButtons/index'
import sysMenu from '@/api/sysMenu'
import sysRoleMenu from '@/api/sysRoleMenu'
import { popAutoHeightDialog, popDialog } from '@/utils/popDialog'
import sysUser from "@/api/sysUser";

export default {
  components: {
    Pagination,
    SearchButtons,
    OperationButtons
  },
  data() {
    return {
      name: 'SysRole',
      searchForm: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      tableData: [],
      currentRow: null,
      dialogProps: {
        label: 'title',
        children: 'child'
      }
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRow = val
    },
    fetchList() {
      sysRole.fetchList({ ...this.searchForm }).then(resp => {
        this.tableData = resp.data.data
        this.total = resp.data.total
        this.searchForm.pageNum = resp.data.pageNum
        this.searchForm.currentPage = resp.data.currentPage
      })
    },
    delSysRoleById(id) {
      sysRole.delById(id).then(resp => {
        this.$message.info(resp.message)
        this.fetchList()
      })
    },
    clearSearchForm() {
      this.searchForm = {}
    },
    downloadTemplate() {
      window.location.href = 'http://localhost:9527/sysRole/template'
    },
    export2Excel() {
      sysRole.export2Excel({ ...this.searchForm })
    },
    showRoleMenu(id) {
      sysMenu.getTree({ id }).then(resp => {
        this.$nextTick(() => {
          const saveBatch = this.saveRoleMenu
          const dialogProps = this.dialogProps
          popAutoHeightDialog('views/sysRole/roleMenuDialog', '770px',
            this.fetchList, { id, resp, saveBatch, dialogProps, title: '编辑角色菜单' })
        })
      })
    },
    showUserRole(id) {
      sysUser.allUser()
    },
    saveRoleMenu(id, ids) {
      sysRoleMenu.saveBatch({ id, ids }).then(resp => {
        this.$message('保存成功')
      })
    },
    addRole() {
      popAutoHeightDialog('views/sysRole/form', '400px',
        this.fetchList, { type: 0, id: null, title: '新增角色' })
    },
    editRole(id) {
      popAutoHeightDialog('views/sysRole/form', '400px',
        this.fetchList, { type: 1, id, title: '编辑角色' })
    },
    saveRoleData() {
      console.log('作者很懒，还没实现功能')
    }
  }
}
</script>
<style>
.dialogBtn{
  margin-top: 30px;
}
.dialogTree{
  margin-bottom: 10px;
  height: 380px;
}
.dialogScrollbar{
  padding: 15px;
}
</style>
